﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace FastAccess
{
    public partial class MainForm : Form
    {
        protected string ProxyDataTxt;

        public ProxyBoxForm _ProxyBox;


        public MainForm()
        {
            InitializeComponent();
            _ProxyBox = new ProxyBoxForm();
        }

        private void ProfileEditButton_Click(object sender, EventArgs e)
        {
             
            
            _ProxyBox.ShowDialog(this);
            
            //MessageBox.Show("此版本不支持");
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (Settings.UseProxy)
            {
                Settings.UseProxy = false;
                txProxyUsePath.Text = "Off";
            }
            else
            {
                Settings.UseProxy = true;
                txProxyUsePath.Text = "On";
            }
        }

        private void GoAndPlayButton_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                MessageBox.Show("正在执行中......");
            }
            else
            {
                int times = 0;
                if (int.TryParse(txRepeat.Text, out times))
                {
                    txStateBox.Text = "开始执行任务";
                    backgroundWorker1.RunWorkerAsync();
                }
                else
                {
                    txRepeat.BackColor = Color.Red;
                }
                
            }
        }

        private void ProcessUrls(object sender, DoWorkEventArgs e)
        {
            string Url = txURL.Text;
            int times = 0;
            int.TryParse(txRepeat.Text, out times);
            if (!Settings.Registed)
            {
                times = 3;
            }
            if (!Url.StartsWith("http"))
            {
                Url = "http://" + Url;
            }
            if (!Settings.UseProxy)
            {
                WebClient wc = new WebClient();
                for (int i = 0; i < times; i++)
                {
                    try
                    {
                        wc.DownloadString(Url);
                        backgroundWorker1.ReportProgress(i * 100 / times, "访问Url:" + Url + " " + (i + 1) + "次");
                    }
                    catch (Exception ex)
                    {
                    }
                }
                backgroundWorker1.ReportProgress(100, "任务完成");
            }
            else
            {
                int success = 0;
                foreach (ProxyDB.ProxyLsRow row in _ProxyBox.Db.ProxyLs)
                {
                    if (Helper.AccessByProxy(row, Url))
                    {
                        success++;
                        backgroundWorker1.ReportProgress(success * 100 / times, "通过代理:" + row.IP + " 访问Url:" + Url + " " + success + "次");
                    }
                    else
                    {
                        backgroundWorker1.ReportProgress(success * 100 / times, "代理:" + row.IP + " 访问" + Url + "失败");
                    }
                    if (success > 9)
                    {
                        break;
                    }
                }
                backgroundWorker1.ReportProgress(100, "任务完成");
            }
        }

        private void UpgradeProgress(object sender, ProgressChangedEventArgs e)
        {
            toolStripProgressBar1.Value = e.ProgressPercentage;
            toolStripProgressBar1.ToolTipText = e.UserState.ToString();
           
            if (e.ToString().StartsWith("操作异常"))
            {
                txStateBox.Text = e.ToString() + "\r\n" + txStateBox.Text;
            }
            else
            {
                txStateBox.Text = e.UserState.ToString() + "\r\n" + txStateBox.Text;
            }
        }

        private void WorkDone(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("工作完成");
            toolStripProgressBar1.Value = 0;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (Settings.Registed)
            {
                MessageBox.Show("Alexander Li 开发 @2009 \r\nRegisted To " + Settings.OutDate.ToShortDateString());
            }
            else
            {
                MessageBox.Show("Alexander Li 开发 @2009 \r\nNot Registed,Send Code ["+Helper.GetKey()+"] To superpowerlee@gmail.com to regist your product");
            }
        }

        private void CancelWorkButton_Click(object sender, EventArgs e)
        {
            toolStripProgressBar1.Value = 0;
            backgroundWorker1.CancelAsync();
            MessageBox.Show("任务被取消");
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            _ProxyBox.LoadData();
            if (!Settings.Registed)
            {
                txStateBox.Text = "未注册版本，一次只能发起3个请求";
            }
        }

        private void OnKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar > '9' || e.KeyChar < '0')
            {
                e.Handled = true;
            }
        }

        private void CheckNum(object sender, EventArgs e)
        {
            if (txRepeat.Text.StartsWith("0"))
            {
                txRepeat.BackColor = Color.Red;
                GoAndPlayButton.Enabled = false;
            }
            else
            {
                txRepeat.BackColor = Color.Green;
                GoAndPlayButton.Enabled = true;
            }

 
        }
    }
}
